package com.cn.qtms.feign.error;//类注释设置模板

import com.alibaba.fastjson.JSON;
import com.cn.qtms.common.result.Result;
import com.cn.qtms.exception.AppException;
import com.cn.qtms.exception.ApplicationException;
import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Name:ErrorDecoder
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-09-05 13:18
 **/
public class ErrorDecoder implements feign.codec.ErrorDecoder {

    private final Logger log = LoggerFactory.getLogger(ErrorDecoder.class);

    @Override
    public Exception decode(String methodKey, Response response) {
        Exception exception = null;
        try {
            //获取原始的返回内容
            InputStream inputStream = response.body().asInputStream();
            Result result = (Result) JSON.parseObject(inputStream, Result.class);
            return new AppException(result.getCode().toString(),result.getMsg());
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
            return new ApplicationException("");
        }
    }
}

